Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.25, n = 716)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 13.00276 13.00043 12.99814 12.99588 12.99365 12.99145 12.98927 12.98712
##   [9] 12.98499 12.98287 12.98078 12.97869 12.97662 12.97455 12.97249 12.97044
##  [17] 12.96838 12.96633 12.96427 12.96220 12.96013 12.95804 12.95594 12.95383
##  [25] 12.95170 12.94954 12.94736 12.94516 12.94293 12.94066 12.93837 12.93603
##  [33] 12.93366 12.93125 12.92880 12.92630 12.92375 12.92114 12.91848 12.91577
##  [41] 12.91302 12.91023 12.90741 12.90456 12.90168 12.89878 12.89587 12.89295
##  [49] 12.89002 12.88708 12.88415 12.88122 12.87831 12.87541 12.87253 12.86967
##  [57] 12.86685 12.86405 12.86130 12.85858 12.85592 12.85330 12.85074 12.84824
##  [65] 12.84580 12.84343 12.84114 12.83892 12.83679 12.83474 12.83279 12.83093
##  [73] 12.82917 12.82751 12.82596 12.82453 12.82321 12.82202 12.82071 12.81906
##  [81] 12.81708 12.81479 12.81223 12.80939 12.80632 12.80302 12.79951 12.79583
##  [89] 12.79198 12.78799 12.78388 12.77967 12.77537 12.77102 12.76663 12.76221
##  [97] 12.75780 12.75341 12.74906 12.74477 12.74057 12.73646 12.73248 12.72865
## [105] 12.72497 12.72148 12.71820 12.71514 12.71232 12.70977 12.70751 12.70555
## [113] 12.70391 12.70263 12.70171 12.70118 12.70106 12.70136 12.70212 12.70334
## [121] 12.70526 12.70805 12.71163 12.71594 12.72092 12.72648 12.73256 12.73910
## [129] 12.74602 12.75326 12.76075 12.76841 12.77618 12.78400 12.79178 12.79947
## [137] 12.80699 12.81428 12.82126 12.82787 12.83405 12.83971 12.84641 12.85557
## [145] 12.86694 12.88028 12.89533 12.91183 12.92955 12.94821 12.96758 12.98741
## [153] 13.00743 13.02740 13.04708 13.06619 13.08451 13.10176 13.11771 13.13210
## [161] 13.14467 13.15519 13.16339 13.17109 13.18019 13.19053 13.20199 13.21441
## [169] 13.22766 13.24159 13.25605 13.27092 13.28605 13.30129 13.31650 13.33155
## [177] 13.34628 13.36056 13.37424 13.38719 13.39926 13.41030 13.42019 13.42877
## [185] 13.43590 13.44144 13.44526 13.44720 13.44712 13.44565 13.44349 13.44066
## [193] 13.43716 13.43299 13.42817 13.42271 13.41660 13.40985 13.40248 13.39449
## [201] 13.38588 13.37667 13.36685 13.35644 13.34544 13.33386 13.32171 13.30899
## [209] 13.29572 13.28188 13.26751 13.25259 13.23542 13.21454 13.19036 13.16326
## [217] 13.13365 13.10192 13.06846 13.03369 12.99799 12.96175 12.92539 12.88929
## [225] 12.85384 12.81946 12.78653 12.75546 12.72663 12.70045 12.67731 12.65454
## [233] 12.62937 12.60208 12.57294 12.54224 12.51024 12.47723 12.44348 12.40926
## [241] 12.37485 12.34053 12.30658 12.27326 12.24086 12.20965 12.17990 12.15190
## [249] 12.12591 12.10222 12.08110 12.06283 12.04718 12.03363 12.02201 12.01214
## [257] 12.00382 11.99690 11.99119 11.98651 11.98267 11.97952 11.97685 11.97450
## [265] 11.97229 11.97004 11.96757 11.96470 11.96126 11.95705 11.95192 11.94566
## [273] 11.93812 11.93071 11.92484 11.92037 11.91714 11.91497 11.91372 11.91323
## [281] 11.91332 11.91385 11.91465 11.91556 11.91642 11.91708 11.91736 11.91712
## [289] 11.91619 11.91441 11.91161 11.90765 11.90236 11.89558 11.88830 11.88154
## [297] 11.87524 11.86929 11.86361 11.85811 11.85270 11.84730 11.84182 11.83617
## [305] 11.83025 11.82399 11.81730 11.81008 11.80225 11.79372 11.78330 11.77005
## [313] 11.75430 11.73634 11.71649 11.69505 11.67234 11.64867 11.62433 11.59965
## [321] 11.57493 11.55048 11.52661 11.50364 11.48185 11.46158 11.44313 11.42680
## [329] 11.41290 11.40175 11.39365 11.38631 11.37739 11.36714 11.35584 11.34374
## [337] 11.33110 11.31819 11.30527 11.29260 11.28044 11.26905 11.25869 11.24963
## [345] 11.24212 11.23643 11.23282 11.23155 11.23288 11.23707 11.24406 11.25347
## [353] 11.26514 11.27889 11.29455 11.31193 11.33087 11.35119 11.37271 11.39526
## [361] 11.41867 11.44275 11.46734 11.49226 11.51734 11.54239 11.56724 11.59173
## [369] 11.61567 11.63889 11.66121 11.68246 11.70518 11.73175 11.76176 11.79478
## [377] 11.83040 11.86820 11.90777 11.94868 11.99051 12.03285 12.07529 12.11739
## [385] 12.15876 12.19895 12.23757 12.27419 12.30839 12.33976 12.36788 12.39232
## [393] 12.41593 12.44163 12.46914 12.49818 12.52847 12.55974 12.59170 12.62408
## [401] 12.65659 12.68897 12.72092 12.75217 12.78245 12.81147 12.83895 12.86462
## [409] 12.88819 12.90940 12.92795 12.94357 12.95598 12.96560 12.97313 12.97871
## [417] 12.98247 12.98455 12.98510 12.98423 12.98211 12.97885 12.97461 12.96951
## [425] 12.96370 12.95731 12.95048 12.94335 12.93605 12.92873 12.92152 12.91455
## [433] 12.90797 12.90192 12.89652 12.88985 12.88008 12.86752 12.85249 12.83530
## [441] 12.81625 12.79567 12.77386 12.75114 12.72782 12.70421 12.68062 12.65736
## [449] 12.63475 12.61310 12.59272 12.57393 12.55703 12.54233 12.53016 12.51837
## [457] 12.50481 12.48976 12.47349 12.45627 12.43839 12.42011 12.40172 12.38349
## [465] 12.36570 12.34862 12.33252 12.31770 12.30441 12.29293 12.28355 12.27507
## [473] 12.26615 12.25689 12.24737 12.23768 12.22791 12.21814 12.20845 12.19894
## [481] 12.18969 12.18079 12.17232 12.16437 12.15702 12.15037 12.14449 12.13948
## [489] 12.13542 12.13239 12.13049 12.12980 12.13051 12.13270 12.13622 12.14095
## [497] 12.14678 12.15356 12.16118 12.16950 12.17841 12.18777 12.19746 12.20735
## [505] 12.21732 12.22724 12.23697 12.24641 12.25541 12.26386 12.27162 12.28040
## [513] 12.29178 12.30551 12.32134 12.33901 12.35826 12.37884 12.40049 12.42295
## [521] 12.44597 12.46929 12.49266 12.51581 12.53849 12.56044 12.58142 12.60115
## [529] 12.61939 12.63587 12.65034 12.66255 12.67224 12.67915 12.68504 12.69178
## [537] 12.69925 12.70734 12.71594 12.72493 12.73419 12.74362 12.75311 12.76252
## [545] 12.77177 12.78072 12.78927 12.79731 12.80471 12.81137 12.81718 12.82201
## [553] 12.82576 12.82831 12.82955 12.82936 12.82763 12.82426 12.81911 12.81209
## [561] 12.80263 12.79041 12.77567 12.75867 12.73964 12.71885 12.69654 12.67295
## [569] 12.64834 12.62295 12.59703 12.57082 12.54458 12.51856 12.49299 12.46814
## [577] 12.44424 12.42154 12.40030 12.38076 12.36317 12.34370 12.31888 12.28947
## [585] 12.25624 12.21996 12.18138 12.14128 12.10042 12.05956 12.01948 11.98093
## [593] 11.94469 11.91151 11.88216 11.85742 11.83803 11.82111 11.80331 11.78480
## [601] 11.76573 11.74624 11.72649 11.70662 11.68679 11.66714 11.64782 11.62899
## [609] 11.61080 11.59339 11.57692 11.56153 11.54738 11.53461 11.52338 11.51384
## [617] 11.50613 11.50040 11.49694 11.49579 11.49676 11.49967 11.50431 11.51050
## [625] 11.51804 11.52674 11.53642 11.54688 11.55792 11.56937 11.58102 11.59268
## [633] 11.60416 11.61527 11.62583 11.63563 11.64448 11.65356 11.66406 11.67587
## [641] 11.68885 11.70287 11.71781 11.73355 11.74994 11.76687 11.78421 11.80182
## [649] 11.81959 11.83738 11.85507 11.87253 11.88962 11.90623 11.92223 11.93748
## [657] 11.95187 11.96525 11.97835 11.99192 12.00593 12.02034 12.03512 12.05023
## [665] 12.06563 12.08129 12.09717 12.11323 12.12944 12.14576 12.16215 12.17858
## [673] 12.19500 12.21139 12.22771 12.24392 12.25998 12.27586 12.29152 12.30714
## [681] 12.32292 12.33885 12.35493 12.37114 12.38749 12.40395 12.42054 12.43723
## [689] 12.45403 12.47091 12.48789 12.50494 12.52206 12.53925 12.55649 12.57378
## [697] 12.59112 12.60848 12.62588 12.64329 12.66072 12.67810 12.69540 12.71263
## [705] 12.72981 12.74696 12.76410 12.78123 12.79839 12.81558 12.83282 12.85013
## [713] 12.86752 12.88502 12.90264 12.92039
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.25, n = 716)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.62494 12.62041 12.61597 12.61163 12.60739 12.60324 12.59919 12.59522
##   [9] 12.59134 12.58755 12.58384 12.58021 12.57667 12.57321 12.56982 12.56651
##  [17] 12.56327 12.56010 12.55701 12.55398 12.55102 12.54812 12.54529 12.54251
##  [25] 12.53980 12.53714 12.53454 12.53199 12.52950 12.52705 12.52466 12.52231
##  [33] 12.52000 12.51774 12.51552 12.51333 12.51118 12.50904 12.50693 12.50485
##  [41] 12.50279 12.50077 12.49879 12.49685 12.49495 12.49311 12.49131 12.48958
##  [49] 12.48790 12.48628 12.48474 12.48326 12.48186 12.48054 12.47931 12.47815
##  [57] 12.47709 12.47612 12.47525 12.47447 12.47381 12.47325 12.47280 12.47247
##  [65] 12.47225 12.47216 12.47220 12.47236 12.47266 12.47310 12.47368 12.47441
##  [73] 12.47528 12.47630 12.47748 12.47883 12.48033 12.48200 12.48381 12.48573
##  [81] 12.48776 12.48989 12.49212 12.49446 12.49691 12.49945 12.50210 12.50484
##  [89] 12.50769 12.51063 12.51367 12.51680 12.52003 12.52336 12.52678 12.53028
##  [97] 12.53388 12.53757 12.54135 12.54522 12.54917 12.55321 12.55733 12.56154
## [105] 12.56583 12.57020 12.57465 12.57918 12.58379 12.58848 12.59325 12.59809
## [113] 12.60301 12.60800 12.61306 12.61819 12.62340 12.62867 12.63401 12.63942
## [121] 12.64500 12.65084 12.65690 12.66318 12.66963 12.67625 12.68300 12.68987
## [129] 12.69682 12.70384 12.71090 12.71798 12.72505 12.73209 12.73908 12.74600
## [137] 12.75281 12.75950 12.76604 12.77241 12.78006 12.79029 12.80284 12.81745
## [145] 12.83386 12.85180 12.87102 12.89126 12.91225 12.93373 12.95544 12.97713
## [153] 12.99853 13.01937 13.03941 13.05837 13.07600 13.09204 13.10622 13.11829
## [161] 13.12798 13.13763 13.14958 13.16355 13.17925 13.19641 13.21475 13.23399
## [169] 13.25385 13.27405 13.29432 13.31438 13.33394 13.35274 13.37048 13.38689
## [177] 13.40169 13.41461 13.42536 13.43367 13.43926 13.44184 13.44214 13.44109
## [185] 13.43874 13.43515 13.43039 13.42450 13.41754 13.40958 13.40066 13.39085
## [193] 13.38019 13.36876 13.35660 13.34377 13.33034 13.31634 13.30186 13.28693
## [201] 13.27161 13.25598 13.24007 13.22395 13.20767 13.19130 13.17488 13.15848
## [209] 13.14215 13.12595 13.10737 13.08428 13.05729 13.02703 12.99412 12.95918
## [217] 12.92283 12.88570 12.84840 12.81157 12.77581 12.74175 12.71002 12.68123
## [225] 12.65601 12.63497 12.61510 12.59311 12.56926 12.54380 12.51695 12.48898
## [233] 12.46013 12.43065 12.40077 12.37075 12.34084 12.31127 12.28230 12.25417
## [241] 12.22713 12.20142 12.17730 12.15499 12.13476 12.11685 12.10151 12.08841
## [249] 12.07700 12.06716 12.05873 12.05161 12.04564 12.04071 12.03667 12.03341
## [257] 12.03078 12.02865 12.02690 12.02539 12.02399 12.02256 12.02099 12.01913
## [265] 12.01685 12.01403 12.01052 12.00621 12.00309 12.00304 12.00576 12.01090
## [273] 12.01816 12.02721 12.03774 12.04941 12.06191 12.07492 12.08812 12.10119
## [281] 12.11379 12.12563 12.13636 12.14568 12.15326 12.15878 12.16192 12.16235
## [289] 12.15976 12.15537 12.15063 12.14554 12.14012 12.13438 12.12834 12.12201
## [297] 12.11540 12.10853 12.10141 12.09405 12.08646 12.07867 12.07067 12.06249
## [305] 12.05414 12.04563 12.03698 12.02819 12.01770 12.00414 11.98782 11.96908
## [313] 11.94823 11.92560 11.90151 11.87629 11.85025 11.82373 11.79703 11.77049
## [321] 11.74443 11.71917 11.69504 11.67235 11.65143 11.63261 11.61620 11.60252
## [329] 11.59192 11.58134 11.56788 11.55204 11.53429 11.51513 11.49504 11.47453
## [337] 11.45408 11.43417 11.41530 11.39796 11.38264 11.36982 11.36001 11.35368
## [345] 11.35133 11.35187 11.35383 11.35714 11.36173 11.36752 11.37444 11.38241
## [353] 11.39135 11.40120 11.41188 11.42332 11.43543 11.44815 11.46140 11.47511
## [361] 11.48920 11.50360 11.51823 11.53302 11.54790 11.56279 11.57962 11.60015
## [369] 11.62402 11.65087 11.68034 11.71206 11.74568 11.78085 11.81719 11.85435
## [377] 11.89198 11.92971 11.96718 12.00404 12.03992 12.07446 12.10731 12.13811
## [385] 12.16649 12.19210 12.21458 12.23650 12.26052 12.28639 12.31384 12.34263
## [393] 12.37251 12.40321 12.43450 12.46611 12.49780 12.52931 12.56039 12.59078
## [401] 12.62024 12.64851 12.67533 12.70046 12.72365 12.74463 12.76316 12.77899
## [409] 12.79353 12.80831 12.82325 12.83825 12.85321 12.86803 12.88264 12.89692
## [417] 12.91080 12.92417 12.93694 12.94901 12.96030 12.97071 12.98015 12.98851
## [425] 12.99572 13.00167 13.00627 13.00942 13.01104 13.01037 13.00688 13.00081
## [433] 12.99239 12.98187 12.96948 12.95547 12.94007 12.92352 12.90606 12.88794
## [441] 12.86938 12.85063 12.83193 12.81351 12.79562 12.77849 12.76237 12.74749
## [449] 12.73409 12.72241 12.70989 12.69408 12.67539 12.65419 12.63089 12.60588
## [457] 12.57955 12.55231 12.52454 12.49665 12.46902 12.44205 12.41614 12.39167
## [465] 12.36906 12.34868 12.33094 12.31623 12.30494 12.29473 12.28309 12.27024
## [473] 12.25635 12.24162 12.22626 12.21045 12.19438 12.17826 12.16228 12.14663
## [481] 12.13151 12.11711 12.10362 12.09124 12.08017 12.07060 12.06272 12.05674
## [489] 12.05283 12.05121 12.05130 12.05237 12.05436 12.05720 12.06081 12.06514
## [497] 12.07010 12.07564 12.08169 12.08817 12.09502 12.10217 12.10955 12.11709
## [505] 12.12473 12.13240 12.14180 12.15453 12.17032 12.18890 12.21002 12.23339
## [513] 12.25877 12.28589 12.31447 12.34426 12.37500 12.40641 12.43823 12.47020
## [521] 12.50206 12.53353 12.56436 12.59428 12.62302 12.65032 12.67592 12.69955
## [529] 12.72095 12.73985 12.75598 12.76909 12.78177 12.79660 12.81333 12.83170
## [537] 12.85144 12.87230 12.89401 12.91631 12.93894 12.96164 12.98414 13.00619
## [545] 13.02752 13.04788 13.06700 13.08461 13.10046 13.11429 13.12584 13.13484
## [553] 13.14103 13.14415 13.14394 13.14072 13.13512 13.12732 13.11750 13.10583
## [561] 13.09250 13.07768 13.06156 13.04431 13.02613 13.00717 12.98764 12.96770
## [569] 12.94753 12.92732 12.90725 12.88749 12.86822 12.84963 12.83190 12.81520
## [577] 12.79688 12.77442 12.74825 12.71878 12.68643 12.65160 12.61472 12.57619
## [585] 12.53644 12.49587 12.45491 12.41396 12.37345 12.33378 12.29538 12.25865
## [593] 12.22401 12.19188 12.16267 12.13680 12.11467 12.09323 12.06931 12.04324
## [601] 12.01533 11.98588 11.95520 11.92362 11.89143 11.85895 11.82649 11.79436
## [609] 11.76287 11.73232 11.70304 11.67534 11.64951 11.62588 11.60475 11.58643
## [617] 11.57124 11.55949 11.55012 11.54183 11.53460 11.52839 11.52317 11.51890
## [625] 11.51554 11.51305 11.51141 11.51058 11.51052 11.51120 11.51258 11.51462
## [633] 11.51729 11.52056 11.52438 11.52873 11.53357 11.53979 11.54818 11.55855
## [641] 11.57073 11.58452 11.59974 11.61620 11.63371 11.65208 11.67114 11.69069
## [649] 11.71054 11.73051 11.75042 11.77007 11.78928 11.80786 11.82562 11.84238
## [657] 11.85796 11.87216 11.88602 11.90065 11.91601 11.93205 11.94871 11.96595
## [665] 11.98371 12.00194 12.02058 12.03960 12.05894 12.07853 12.09835 12.11832
## [673] 12.13841 12.15856 12.17871 12.19883 12.21885 12.23872 12.25840 12.27815
## [681] 12.29824 12.31867 12.33944 12.36051 12.38188 12.40354 12.42548 12.44767
## [689] 12.47012 12.49280 12.51570 12.53882 12.56213 12.58562 12.60929 12.63311
## [697] 12.65708 12.68118 12.70540 12.72972 12.75415 12.77855 12.80286 12.82710
## [705] 12.85130 12.87550 12.89974 12.92403 12.94841 12.97291 12.99757 13.02241
## [713] 13.04747 13.07277 13.09836 13.12425
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.25, n = 716)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 11.99375 11.99010 11.98652 11.98301 11.97955 11.97615 11.97281 11.96952
##   [9] 11.96627 11.96307 11.95992 11.95680 11.95372 11.95067 11.94765 11.94466
##  [17] 11.94170 11.93875 11.93583 11.93292 11.93002 11.92713 11.92425 11.92137
##  [25] 11.91850 11.91562 11.91273 11.90984 11.90693 11.90401 11.90107 11.89811
##  [33] 11.89513 11.89212 11.88908 11.88601 11.88291 11.87976 11.87658 11.87335
##  [41] 11.87007 11.86674 11.86336 11.85989 11.85633 11.85267 11.84892 11.84510
##  [49] 11.84121 11.83726 11.83327 11.82924 11.82517 11.82109 11.81699 11.81289
##  [57] 11.80879 11.80471 11.80066 11.79664 11.79266 11.78873 11.78486 11.78106
##  [65] 11.77734 11.77371 11.77018 11.76675 11.76344 11.76025 11.75719 11.75428
##  [73] 11.75151 11.74891 11.74648 11.74422 11.74216 11.74029 11.73863 11.73718
##  [81] 11.73595 11.73496 11.73421 11.73371 11.73347 11.73322 11.73267 11.73186
##  [89] 11.73081 11.72954 11.72809 11.72647 11.72471 11.72284 11.72089 11.71886
##  [97] 11.71681 11.71473 11.71267 11.71065 11.70869 11.70682 11.70506 11.70343
## [105] 11.70197 11.70070 11.69965 11.69883 11.69827 11.69801 11.69805 11.69844
## [113] 11.69919 11.70033 11.70189 11.70388 11.70634 11.70929 11.71276 11.71676
## [121] 11.72133 11.72649 11.73227 11.73869 11.74577 11.75354 11.76323 11.77581
## [129] 11.79089 11.80812 11.82714 11.84756 11.86904 11.89119 11.91366 11.93608
## [137] 11.95808 11.97930 11.99936 12.01791 12.03457 12.04898 12.06394 12.08226
## [145] 12.10361 12.12763 12.15400 12.18236 12.21238 12.24371 12.27601 12.30895
## [153] 12.34218 12.37536 12.40814 12.44019 12.47116 12.50072 12.52851 12.55421
## [161] 12.57746 12.59793 12.61528 12.63170 12.64950 12.66853 12.68863 12.70962
## [169] 12.73135 12.75365 12.77636 12.79931 12.82235 12.84531 12.86803 12.89034
## [177] 12.91208 12.93309 12.95320 12.97226 12.99009 13.00654 13.02144 13.03463
## [185] 13.04594 13.05522 13.06230 13.06702 13.06920 13.06916 13.06736 13.06389
## [193] 13.05884 13.05232 13.04441 13.03521 13.02481 13.01331 13.00081 12.98739
## [201] 12.97316 12.95820 12.94261 12.92649 12.90992 12.89301 12.87585 12.85853
## [209] 12.84114 12.82379 12.80656 12.78956 12.77006 12.74568 12.71696 12.68445
## [217] 12.64871 12.61028 12.56973 12.52760 12.48445 12.44082 12.39728 12.35437
## [225] 12.31264 12.27265 12.23494 12.20008 12.16861 12.14109 12.11807 12.09688
## [233] 12.07460 12.05136 12.02732 12.00262 11.97739 11.95179 11.92596 11.90004
## [241] 11.87419 11.84853 11.82322 11.79841 11.77423 11.75083 11.72836 11.70695
## [249] 11.68676 11.66793 11.65059 11.63491 11.62099 11.60876 11.59808 11.58880
## [257] 11.58078 11.57388 11.56795 11.56286 11.55845 11.55459 11.55113 11.54793
## [265] 11.54484 11.54173 11.53845 11.53485 11.53080 11.52615 11.52075 11.51447
## [273] 11.50716 11.50047 11.49602 11.49356 11.49287 11.49373 11.49590 11.49915
## [281] 11.50327 11.50801 11.51316 11.51848 11.52374 11.52873 11.53320 11.53694
## [289] 11.53970 11.54128 11.54143 11.53992 11.53654 11.53105 11.52373 11.51511
## [297] 11.50537 11.49467 11.48319 11.47111 11.45858 11.44579 11.43291 11.42011
## [305] 11.40755 11.39541 11.38387 11.37309 11.36324 11.35451 11.34461 11.33143
## [313] 11.31531 11.29664 11.27578 11.25309 11.22894 11.20370 11.17774 11.15142
## [321] 11.12511 11.09917 11.07398 11.04990 11.02729 11.00653 10.98798 10.97200
## [329] 10.95897 10.94926 10.94321 10.93942 10.93618 10.93354 10.93149 10.93005
## [337] 10.92924 10.92907 10.92955 10.93071 10.93255 10.93509 10.93835 10.94233
## [345] 10.94706 10.95254 10.95880 10.96585 10.97369 10.98236 10.99274 11.00561
## [353] 11.02076 11.03799 11.05711 11.07791 11.10020 11.12376 11.14842 11.17396
## [361] 11.20018 11.22689 11.25388 11.28096 11.30792 11.33457 11.36071 11.38613
## [369] 11.41064 11.43403 11.45611 11.47668 11.49771 11.52112 11.54667 11.57406
## [377] 11.60304 11.63334 11.66470 11.69683 11.72948 11.76238 11.79526 11.82784
## [385] 11.85988 11.89108 11.92120 11.94995 11.97708 12.00230 12.02537 12.04600
## [393] 12.06593 12.08694 12.10889 12.13161 12.15494 12.17872 12.20279 12.22700
## [401] 12.25118 12.27517 12.29881 12.32194 12.34441 12.36605 12.38670 12.40621
## [409] 12.42441 12.44114 12.45625 12.46957 12.48094 12.49078 12.49961 12.50748
## [417] 12.51445 12.52055 12.52582 12.53032 12.53409 12.53717 12.53961 12.54146
## [425] 12.54275 12.54354 12.54386 12.54377 12.54331 12.54252 12.54144 12.54014
## [433] 12.53864 12.53699 12.53524 12.53233 12.52729 12.52032 12.51163 12.50140
## [441] 12.48985 12.47718 12.46357 12.44923 12.43437 12.41917 12.40385 12.38859
## [449] 12.37361 12.35909 12.34524 12.33227 12.32035 12.30971 12.30053 12.29127
## [457] 12.28038 12.26809 12.25464 12.24027 12.22519 12.20965 12.19387 12.17809
## [465] 12.16254 12.14745 12.13305 12.11957 12.10726 12.09633 12.08702 12.07792
## [473] 12.06758 12.05616 12.04382 12.03072 12.01702 12.00288 11.98846 11.97393
## [481] 11.95943 11.94513 11.93120 11.91779 11.90506 11.89317 11.88229 11.87257
## [489] 11.86417 11.85726 11.85199 11.84852 11.84611 11.84393 11.84199 11.84033
## [497] 11.83897 11.83796 11.83730 11.83704 11.83721 11.83782 11.83892 11.84053
## [505] 11.84267 11.84538 11.84869 11.85262 11.85721 11.86248 11.86846 11.87613
## [513] 11.88630 11.89875 11.91324 11.92956 11.94747 11.96676 11.98720 12.00857
## [521] 12.03063 12.05317 12.07595 12.09876 12.12137 12.14355 12.16508 12.18573
## [529] 12.20528 12.22351 12.24018 12.25508 12.26798 12.27865 12.28909 12.30133
## [537] 12.31518 12.33047 12.34702 12.36464 12.38315 12.40237 12.42212 12.44223
## [545] 12.46250 12.48276 12.50283 12.52253 12.54167 12.56007 12.57756 12.59395
## [553] 12.60906 12.62271 12.63473 12.64492 12.65310 12.65911 12.66275 12.66384
## [561] 12.66265 12.65965 12.65497 12.64877 12.64119 12.63236 12.62243 12.61154
## [569] 12.59983 12.58744 12.57452 12.56121 12.54765 12.53398 12.52034 12.50688
## [577] 12.49374 12.48105 12.46897 12.45762 12.44717 12.43478 12.41797 12.39732
## [585] 12.37344 12.34691 12.31834 12.28832 12.25746 12.22634 12.19557 12.16575
## [593] 12.13746 12.11130 12.08789 12.06780 12.05164 12.03659 12.01959 12.00089
## [601] 11.98073 11.95934 11.93697 11.91386 11.89025 11.86638 11.84250 11.81884
## [609] 11.79565 11.77316 11.75163 11.73128 11.71237 11.69512 11.67980 11.66662
## [617] 11.65584 11.64770 11.64174 11.63726 11.63417 11.63234 11.63167 11.63205
## [625] 11.63339 11.63556 11.63846 11.64198 11.64602 11.65046 11.65520 11.66013
## [633] 11.66515 11.67014 11.67499 11.67961 11.68388 11.68864 11.69476 11.70210
## [641] 11.71054 11.71997 11.73027 11.74131 11.75298 11.76516 11.77772 11.79055
## [649] 11.80352 11.81652 11.82943 11.84212 11.85448 11.86638 11.87772 11.88835
## [657] 11.89818 11.90707 11.91569 11.92473 11.93418 11.94399 11.95414 11.96459
## [665] 11.97530 11.98626 11.99741 12.00873 12.02019 12.03175 12.04338 12.05505
## [673] 12.06672 12.07836 12.08995 12.10143 12.11280 12.12400 12.13500 12.14598
## [681] 12.15710 12.16837 12.17978 12.19131 12.20296 12.21473 12.22660 12.23858
## [689] 12.25064 12.26280 12.27503 12.28733 12.29969 12.31212 12.32459 12.33710
## [697] 12.34965 12.36223 12.37483 12.38744 12.40006 12.41263 12.42509 12.43746
## [705] 12.44977 12.46203 12.47425 12.48647 12.49868 12.51092 12.52319 12.53553
## [713] 12.54793 12.56043 12.57304 12.58578
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")